package cn.funeralobjects.ams.service.impl;

import cn.funeralobjects.ams.entity.EProduct;
import cn.funeralobjects.ams.entity.EProductRes;
import cn.funeralobjects.ams.entity.ERegion;
import cn.funeralobjects.ams.model.ProductRes;
import cn.funeralobjects.ams.repository.BaseAmsRepository;
import cn.funeralobjects.ams.repository.EProductResRepository;
import cn.funeralobjects.ams.service.ProductResService;
import cn.funeralobjects.ams.service.ProductService;
import cn.funeralobjects.ams.service.RegionService;
import cn.funeralobjects.common.jpa.service.JpaEnableService;
import cn.funeralobjects.common.jpa.service.JpaNameService;
import cn.funeralobjects.common.repository.EnableRepository;
import cn.funeralobjects.common.repository.NameRepository;
import cn.funeralobjects.common.service.annotataion.CommonService;
import cn.funeralobjects.common.service.exception.DataConflictException;
import cn.funeralobjects.common.service.validate.DataCheck;
import cn.funeralobjects.util.Assert;
import cn.funeralobjects.util.NumberUtils;

import javax.annotation.Resource;
import java.util.Optional;

/**
 * @author FuneralObjects
 * Create date: 2020/5/25 5:01 PM
 */
@CommonService
public class ProductResServiceImpl implements ProductResService, BaseJpaAmsService<ProductRes, EProductRes, Integer>, JpaNameService<EProductRes, Integer>, JpaEnableService<EProductRes, Integer> {
    @Resource
    private EProductResRepository eProductResRepository;

    @Resource
    private ProductResService productResService;

    @Resource
    private ProductService productService;

    @Resource
    private RegionService regionService;

    @Override
    public BaseAmsRepository<EProductRes, Integer> baseAmsRepository() {
        return eProductResRepository;
    }

    @Override
    public void checkConflict(ProductRes productRes) throws DataConflictException {
        DataCheck.conflictData(this.existsByName(productRes.getName()), () -> createDataCode("name", productRes.getName()));
    }

    @Override
    public EProductRes toEntity(ProductRes productRes) {
        return Optional.ofNullable(productRes)
                .map(modify -> {
                    EProduct product = new EProduct();
                    ERegion region = new ERegion();
                    region.setId(productRes.getRegionId());
                    product.setId(productRes.getProductId());
                    return new EProductRes().setCode(productRes.getCode())
                            .setName(productRes.getName())
                            .setProduct(product)
                            .setRegion(region);
                }).orElse(null);

    }

    @Override
    public NameRepository<EProductRes, Integer> nameRepository() {
        return eProductResRepository;
    }

    @Override
    public void checkConflict(ProductRes productRes, Integer withoutId) throws DataConflictException {
        DataCheck.conflictData(this.existsByName(productRes.getName(), withoutId), () -> createDataCode("name", productRes.getName()));
    }

    @Override
    public void copyToEntity(ProductRes sourceModify, EProductRes targetEntity) {
        ERegion eRegion = new ERegion();
        eRegion.setId(sourceModify.getRegionId());
        EProduct eProduct = new EProduct();
        eProduct.setId(sourceModify.getProductId());

        targetEntity.setCode(sourceModify.getCode())
                .setName(sourceModify.getName())
                .setRegion(eRegion)
                .setProduct(eProduct);
    }

    @Override
    public boolean isValid(Integer id) {
        return NumberUtils.notZero(id);
    }


    @Override
    public void validateModify(ProductRes productRes) {
        Assert.argAssert(productRes, "productRes");
        productService.mustExistsById(productRes.getProductId());
        regionService.mustExistsById(productRes.getRegionId());
    }

    @Override
    public EnableRepository<EProductRes, Integer> enableRepository() {
        return eProductResRepository;
    }
}
